package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.votes.Request.CreateVotesRequest;
import com.admin.education.dataTransferObject.votes.Request.UpdateVotesRequest;
import com.admin.education.dataTransferObject.votes.VotesTransfer;
import com.admin.education.services.Interfaces.IVotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@RestController("adminVotesController")
@RequestMapping("votes")
public class VotesController {
    @Autowired
    private IVotesService votesService;
    @PostMapping("")
    public Callable<IResponseMessage> createVotes(@Valid @RequestBody CreateVotesRequest createVotesRequest){
        return ()->{
            IResponseMessage responseMessage= null;

            ServicesExcuteResult result =this.votesService.creaateVotes(createVotesRequest);
            if(result.isSuccess()){
                responseMessage =new ResponseMessage(Result.Success);
            }
            else{
                throw new BadRequestException(result.getErrorMessage());
            }
            return responseMessage;
        };
    }
    @DeleteMapping("{id}")
    public Callable<IResponseMessage> deleteVotes(@PathVariable Long id){
        return ()->{

            IResponseMessage responseMessage= null;

            ServicesExcuteResult result =this.votesService.deleteVotes(id);
            if(result.isSuccess()){
                responseMessage =new ResponseMessage(Result.Success);
            }
            else{
                throw new BadRequestException(result.getErrorMessage());
            }
            return responseMessage;
        };
    }
    @PutMapping("{id}")
    public Callable<IResponseMessage> updateVotes(@PathVariable Long id, @RequestBody UpdateVotesRequest updateVotesRequest){
        return ()->{
            IResponseMessage responseMessage =null;

            ServicesExcuteResult result = this.votesService.updateVotes(id,updateVotesRequest);
            if(result.isSuccess()){
                responseMessage = new ResponseMessage(Result.Success);
            }
            else{
                throw new BadRequestException(result.getErrorMessage());
            }
            return responseMessage;
        };
    }
    @GetMapping("{id}")
    public Callable<IResponseMessage> getVotesById(@PathVariable Long id){
        return ()->{
            IResponseMessage responseMessage =null;

            ServicesExcuteResult<VotesTransfer> result = this.votesService.queryVotesById(id);
            if(result.isSuccess()){
                responseMessage = new ResponseMessageWithData<>(Result.Success,result.getData());
            }
            else{
                throw new BadRequestException(result.getErrorMessage());
            }
            return responseMessage;
        };
    }
    @GetMapping("")
    public Callable<IResponseMessage> queryVotes(@Valid PageRequest pageRequest){
        return ()->{
            IResponseMessage responseMessage = null;

            ServicesExcuteResult<PageResponse<VotesTransfer>> result = this.votesService.queryVotes(pageRequest);
            if(result.isSuccess()){
                responseMessage = new ResponseMessageWithData<>(Result.Success,result.getData());
            }
            else{
                throw new BadRequestException(result.getErrorMessage());
            }
            return responseMessage;
        };
    }
}
